package com.san.metadata.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.san.common.utils.CTConversionUtils;
import com.san.common.utils.Constant;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.metadata.dao.ManageMetadataDao;
import com.san.metadata.dao.ManageMetadataManager;
import com.san.metadata.domain.ColumnDTO;
import com.san.metadata.domain.DataSourceDO;
import com.san.metadata.domain.ManageColumnsDO;
import com.san.metadata.domain.ManageTableDO;
import com.san.metadata.domain.TableDTO;
import com.san.metadata.service.ManageMetadataService;
import com.san.metadata.vo.ManageColumnsVO;

/**
 * 技术元数据服务实现类
 * 
 * @author hulp
 *
 */
@Service
public class ManageMetadataServiceImpl implements ManageMetadataService {

	private static final Logger logger = LoggerFactory.getLogger(ManageMetadataServiceImpl.class);

	@Autowired
	private ManageMetadataDao manageMetadataMapper;

	@Autowired
	private ManageMetadataManager manageMetadataManager;

	@Override
	public PageUtils list(Query query) throws Exception {
		List<ManageTableDO> list = manageMetadataMapper.list(query);
		int total = manageMetadataMapper.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}

	@Override
	public List<TableDTO> listManageTable() throws Exception {
		return manageMetadataMapper.listTable();
	}

	@Override
	public List<ColumnDTO> listManageColumns(Long tableId) throws Exception {
		return manageMetadataMapper.listColumns(tableId);
	}

	@Override
	public int save(ManageTableDO messageMetadataDO) throws Exception {
		return manageMetadataMapper.save(messageMetadataDO);
	}

	@Override
	public int doEdit(ManageTableDO messageMetadataDO) throws Exception {
		return manageMetadataMapper.doEdit(messageMetadataDO);
	}

	@Override
	public int remove(Long tableId) throws Exception {
		return manageMetadataMapper.remove(tableId);
	}

	@Override
	public int batchRemove(Long[] tableIds) throws Exception {
		return manageMetadataMapper.batchRemove(tableIds);
	}

	@Override
	public ManageTableDO getById(Long tableId) throws Exception {
		return manageMetadataMapper.getById(tableId);
	}

	@Override
	public int saveColumn(ManageColumnsVO messageColumnsVO) throws Exception {
		return manageMetadataMapper.saveColumn(messageColumnsVO);
	}

	@Override
	@Transactional
	public boolean saveTable(int sourceType, Long tableId, DataSourceDO targetDataSourceDO,
			List<ManageColumnsVO> manageColumnsVOList) {
		ManageTableDO manageTableDO = manageMetadataMapper.getById(tableId);
		int dataType = 0;
		for (ManageColumnsVO i : manageColumnsVOList) {

			// 如果属于涉密信息 则 进行字段类型转换
			if (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_INPUT_SECRET + ",") != -1) {
				i.setColumnType("TEXT");
			}
			
			if (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_ID_CARD + ",") != -1 || i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_ID_CARD1 + ",") != -1) {
				i.setColumnType("VARCHAR");
				i.setColumnLength((long) 100);
			}

			String[] columnType = CTConversionUtils
					.O2M(i.getColumnType(), i.getColumnLength(), i.getColumnPrecision(), i.getColumnScale())
					.split("\\(");
			i.setColumnType(columnType[0]);
			i.setColumnName(i.getColumnName().toUpperCase());
			manageMetadataMapper.saveColumn(i);

			if (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_LEGAL_PERSON + ",") != -1
					&& i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON + ",") != -1) {
				dataType = 3; // 法人 或 自然人
			} else if (dataType == 1 && i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON + ",") != -1) {
				dataType = 4; // 法人 + 自然人
			} else if (dataType == 2 && i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_LEGAL_PERSON + ",") != -1) {
				dataType = 4; // 法人 + 自然人
			} else if (dataType == 2 && (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON + ",") != -1
					|| i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON1 + ",") != -1)) {
				dataType = 5; // 自然人 + 自然人
			} else if (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_LEGAL_PERSON + ",") != -1) {
				dataType = 1; // 法人
			} else if (i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON + ",") != -1
					|| i.getColumnTag().indexOf(Constant.COLUMN_TAG_TYPE_NATURAL_PERSON1 + ",") != -1) {
				dataType = 2; // 自然人
			}

		}

		ManageColumnsVO manageColumnsVO;
		switch (dataType) { // 根据表类型 添加对应的必要字段
		case Constant.MANAGE_DATA_TYPE_LEGAL_PERSON: {
			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("LEGAL_PERSON_ID");
			manageColumnsVO.setColumnComment("法人ID");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);
		}
			break;
		case Constant.MANAGE_DATA_TYPE_NATURAL_PERSON: {
			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("NATURAL_PERSON_ID");
			manageColumnsVO.setColumnComment("自然人ID");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);
		}
			break;
		case Constant.MANAGE_DATA_TYPE_LEGAL_PERSON_OR_NATURAL_PERSON:
		case Constant.MANAGE_DATA_TYPE_LEGAL_PERSON_AND_NATURAL_PERSON: {
			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("LEGAL_PERSON_ID");
			manageColumnsVO.setColumnComment("法人ID");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);

			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("NATURAL_PERSON_ID");
			manageColumnsVO.setColumnComment("自然人ID");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);
		}
			break;
		case Constant.MANAGE_DATA_TYPE_NATURAL_PERSON_OR_NATURAL_PERSON: {
			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("NATURAL_PERSON_ID");
			manageColumnsVO.setColumnComment("自然人ID");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);

			manageColumnsVO = new ManageColumnsVO();
			manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
			manageColumnsVO.setColumnName("NATURAL_PERSON_ID1");
			manageColumnsVO.setColumnComment("自然人ID1");
			manageColumnsVO.setColumnType("VARCHAR");
			manageColumnsVO.setColumnLength((long) 50);
			manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
			manageColumnsVO.setColumnIsNullable("YES");
			manageColumnsVOList.add(manageColumnsVO);
		}
			break;

		default:
			break;
		}

		// 添加必要字段 REPEAT_MARK
		manageColumnsVO = new ManageColumnsVO();
		manageColumnsVO.setTableId(manageColumnsVOList.get(0).getTableId());
		manageColumnsVO.setColumnName("REPEAT_MARK");
		manageColumnsVO.setColumnComment("去重字段");
		manageColumnsVO.setColumnType("VARCHAR");
		manageColumnsVO.setColumnLength((long) 50);
		manageColumnsVO.setCreateUserId(manageColumnsVOList.get(0).getCreateUserId());
		manageColumnsVO.setColumnIsNullable("YES");
		manageColumnsVOList.add(manageColumnsVO);

		boolean flag = manageMetadataManager.saveTable(sourceType, manageTableDO, targetDataSourceDO,
				manageColumnsVOList);
		if (flag != true) {
			logger.error("管理元数据建表失败");
			throw new RuntimeException();
		}
		manageTableDO.setCreateFlag((long) 0);
		manageTableDO.setDataType((long) dataType);
		try {
			manageMetadataMapper.doEdit(manageTableDO);
		} catch (Exception e) {
			logger.error("更新技术元数据状态失败", e);
		}
		return flag;
	}

	@Override
	public ColumnDTO getColumnById(Long columnId) throws Exception {
		return manageMetadataMapper.getColumnById(columnId);
	}


	@Override
	public List<ManageColumnsDO> allManageColumns(Long tableId) throws Exception {
		return manageMetadataMapper.allManageColumns(tableId);
	}

	@Override
	public List<TableDTO> listTable1(int dataType) throws Exception {
		return manageMetadataMapper.listTable1(dataType);
	}

	@Override
	public List<TableDTO> listTable2(int[] dataType) throws Exception {
		return manageMetadataMapper.listTable2(dataType);
	}

	@Override
	public int batchColumnsUpdate(List<ManageColumnsVO> ManageColumnsVOList) {
		return manageMetadataMapper.batchColumnsUpdate(ManageColumnsVOList);
	}

	@Override
	public List<String> listColumnByIds(String columnIds) throws Exception {
		return manageMetadataMapper.listColumnByIds(columnIds);
	}

	@Override
	public ManageTableDO getByName(String name) throws Exception {
		return manageMetadataMapper.getByName(name);
	}

}
